﻿using DotNetty.Transport.Channels;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;

namespace NetC100KTest
{
    /// <summary>
    /// 函数调用流程
    /// 打开：HandlerAdded => ChannelRegistered => ChannelActive
    /// 读取（操作循环）：ChannelRead => ChannelReadComplete
    /// 结束：ChannelInactive => ChannelUnregistered => HandlerRemoved
    /// </summary>
    public class SessionHandler : ChannelDuplexHandler
    {
        public SessionHandler()
        {
        }

        /// <summary>
        /// 客户端连接触发3
        /// </summary>
        /// <param name="context"></param>
        public override void ChannelActive(IChannelHandlerContext context)
        {
            Console.WriteLine("连接：ChannelActive>>>" + context.Channel.LocalAddress + ">>>" + context.Channel.RemoteAddress);

            //if (!Channels.ContainsKey(context.Channel.Id.ToString()))
            //{
            //    if (Channels.TryAdd(context.Channel.Id.ToString(), context.Channel))
            //    {
            //        Console.WriteLine("连接添加成功，连接ID：" + context.Channel.Id.ToString());
            //    }
            //    else
            //    {
            //        Console.WriteLine("连接添加失败，连接ID：" + context.Channel.Id.ToString());
            //    }
            //}
        }

        /// <summary>
        /// 客户端断开触发1
        /// </summary>
        /// <param name="context"></param>
        public override void ChannelInactive(IChannelHandlerContext context)
        {
            Console.WriteLine("断开：ChannelInactive>>>" + context.Channel.LocalAddress + ">>>" + context.Channel.RemoteAddress);

            //if (Channels.ContainsKey(context.Channel.Id.ToString()))
            //{
            //    if (!Channels.TryRemove(context.Channel.Id.ToString(), out IChannel channel))
            //    {
            //        Console.WriteLine("连接移除失败，连接ID：" + context.Channel.Id.ToString());
            //    }
            //}
            //else
            //{
            //    Console.WriteLine("发现未知连接，连接ID：" + context.Channel.Id.ToString());
            //}
        }

        /// <summary>
        /// 消息读取触发1
        /// </summary>
        /// <param name="context"></param>
        /// <param name="message"></param>
        public override void ChannelRead(IChannelHandlerContext context, object message)
        {
            //Console.WriteLine(">>>" + context.Channel.Id + ">>>" + nameof(ChannelRead));
        }

        /// <summary>
        /// 消息读取触发2
        /// </summary>
        /// <param name="context"></param>
        public override void ChannelReadComplete(IChannelHandlerContext context)
        {
            //Console.WriteLine(">>>" + context.Channel.Id + ">>>" + nameof(ChannelReadComplete));
        }

        /// <summary>
        /// 客户端连接触发2
        /// </summary>
        /// <param name="context"></param>
        public override void ChannelRegistered(IChannelHandlerContext context)
        {
            Console.WriteLine("连接：ChannelRegistered>>>" + context.Channel.LocalAddress + ">>>" + context.Channel.RemoteAddress);
        }

        /// <summary>
        /// 客户端断开触发2
        /// </summary>
        /// <param name="context"></param>
        public override void ChannelUnregistered(IChannelHandlerContext context)
        {
            Console.WriteLine("断开：ChannelUnregistered>>>" + context.Channel.LocalAddress + ">>>" + context.Channel.RemoteAddress);
        }

        /// <summary>
        /// 客户端连接触发1
        /// </summary>
        /// <param name="context"></param>
        public override void HandlerAdded(IChannelHandlerContext context)
        {
            Console.WriteLine("连接：HandlerAdded>>>" + context.Channel.LocalAddress + ">>>" + context.Channel.RemoteAddress);
        }

        /// <summary>
        /// 客户端断开触发3
        /// </summary>
        /// <param name="context"></param>
        public override void HandlerRemoved(IChannelHandlerContext context)
        {
            Console.WriteLine("断开：HandlerRemoved>>>" + context.Channel.LocalAddress + ">>>" + context.Channel.RemoteAddress);
        }
    }
}
